﻿#include "EasyEpollServer.h"

#if __linux__

#include "CELLEpoll.hpp"
#include "CELLLog.hpp"
#include "CELLEpollServer.h"

void EasyEpollServer::Start(int nCELLServer)
{
	EasyTcpServer::Start<CELLEpollServer>(nCELLServer);
}

// 处理网络消息
void EasyEpollServer::OnRun(CELLThread* pthread)
{
	CELLEpoll ep;
	ep.create(1);
	ep.ctl(EPOLL_CTL_ADD, sockfd(), EPOLLIN);
	while (pthread->isRun())
	{
		time4msg();		

		int ret = ep.wait(1);
		if (ret < 0)
		{
			CELLLog_pError("EasyEpollServer::OnRun error");
			pthread->Exit();
			break;
		}
		auto events = ep.events();
		for (int i = 0; i < ret; ++i)
		{
			if (events[i].data.fd == sockfd())
			{
				if (events[i].events & EPOLLIN)
				{
					Accept();
				}
			}
		}
	}
	CELLLog_Info("EasyEpollServer OnRun exit");
}

#endif // #if __linux__
